package com.grapes.view;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.grapes.R;
import com.grapes.business.RecruiteInfoBusiness;
import com.grapes.model.DailyInfo;
import com.grapes.widget.PullToRefreshListView;

public class DailyInfoListView extends PullToRefreshListView{

	private int selectedDailyInfoIndex=-1;
	private List<DailyInfo> dailyInfos=new LinkedList<DailyInfo>();
	private DailyInfoAdapter adapter;
	
	public DailyInfoListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public DailyInfoListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public DailyInfoListView(Context context) {
		super(context);
		init();
	}
	
	private void init()
	{
		adapter=new DailyInfoAdapter();
		setAdapter(adapter);
	}
	
	public DailyInfoAdapter getListViewAdapter()
	{
		return adapter;
	}
	//TODO
	public void reloadData()
	{
		new Thread(new Runnable() {
			@Override
			public void run() {
				dailyInfos = RecruiteInfoBusiness.getInstance()
						.getDailyInfoBefore(System.currentTimeMillis());
				Message msg = new Message();
				msg.what = RecruiteInfoListActivity.MSG_DAILY_INFO_ALREADY;
				((RecruiteInfoListActivity)getController()).getControllerHandler().sendMessage(msg);
			}
		}).start();
	}
	
		
	private void shareDailyInfo()
	{
		
	}
	
	private void refuseDailyInfo()
	{
		
	}
	
	private void favoriteDailyInfo()
	{
		
	}
	
	private class CellViewHolder {
		TextView companyNameTxt;
		TextView timeThresholdTxt;
		TextView detailTxt;
		TextView favoriteCountTxt;
		TextView commentCountTxt;
		Button shareBtn;
		Button refuseBtn;
		Button favoriteBtn;
		RelativeLayout upperGroup;
		RelativeLayout middleGroup;
		RelativeLayout bottomGroup;
	}
	
	class DailyInfoAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return dailyInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}


		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			CellViewHolder viewHolder;
			TextView companyNameTxt;
			TextView timeThresholdTxt;
			TextView detailTxt;
			TextView favoriteCountTxt;
			TextView commentCountTxt;
			Button shareBtn;
			Button refuseBtn;
			Button favoriteBtn;
			final RelativeLayout upperGroup;
			final RelativeLayout middleGroup;
			final RelativeLayout bottomGroup;

			if (convertView == null) {
				convertView = ((RecruiteInfoListActivity)getController()).getLayoutInflater().inflate(
						R.layout.daily_info_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.timeThresholdTxt = (TextView) convertView
						.findViewById(R.id.dailly_info_time_threshold_txt);
				viewHolder.companyNameTxt = (TextView) convertView
						.findViewById(R.id.daily_info_company_name_txt);
				viewHolder.detailTxt = (TextView) convertView
						.findViewById(R.id.daily_info_detail_txt);
				viewHolder.favoriteCountTxt = (TextView) convertView
						.findViewById(R.id.favorite_count_txt);
				viewHolder.commentCountTxt = (TextView) convertView
						.findViewById(R.id.comment_count_txt);
				// Button
				viewHolder.shareBtn = (Button) convertView
						.findViewById(R.id.share_btn);
				viewHolder.refuseBtn = (Button) convertView
						.findViewById(R.id.refuse_btn);
				viewHolder.favoriteBtn = (Button) convertView
						.findViewById(R.id.favorite_btn);
				// ViewGroup
				viewHolder.upperGroup = (RelativeLayout) convertView
						.findViewById(R.id.daily_info_upper_group);
				viewHolder.middleGroup = (RelativeLayout) convertView
						.findViewById(R.id.daily_info_btn_group);
				viewHolder.bottomGroup = (RelativeLayout) convertView
						.findViewById(R.id.daily_info_bottom_group);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			companyNameTxt = viewHolder.companyNameTxt;
			timeThresholdTxt = viewHolder.timeThresholdTxt;
			detailTxt = viewHolder.detailTxt;
			favoriteCountTxt = viewHolder.favoriteCountTxt;
			commentCountTxt = viewHolder.commentCountTxt;
			shareBtn = viewHolder.shareBtn;
			refuseBtn = viewHolder.refuseBtn;
			favoriteBtn = viewHolder.favoriteBtn;
			upperGroup = viewHolder.upperGroup;
			middleGroup = viewHolder.middleGroup;
			bottomGroup = viewHolder.bottomGroup;

			DailyInfo dailyInfo = dailyInfos.get(position);
			companyNameTxt.setText(dailyInfo.getCompanyName());
			detailTxt.setText(dailyInfo.getDetail());
			//timeThresholdTxt.setText(dailyInfo.getDate()+" "+dailyInfo.getTime());
			favoriteCountTxt.setText(String.valueOf(dailyInfo
					.getFavoriteCount()));
			commentCountTxt.setText(String.valueOf(dailyInfo
					.getCommentCount()));
			if(position==selectedDailyInfoIndex)
			{
			    middleGroup.setVisibility(View.VISIBLE);
			}else
				middleGroup.setVisibility(View.GONE);
			

			View.OnClickListener onRegionClickListener=new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					int height=middleGroup.getHeight()/2;
					Animation.AnimationListener cellAnimationListener=new Animation.AnimationListener()
					{
						@Override
						public void onAnimationEnd(Animation animation) {
							adapter.notifyDataSetInvalidated();
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
							
						}

						@Override
						public void onAnimationStart(Animation animation) {
							
						}					
					};
					
					if(position==selectedDailyInfoIndex)
					{
						//to close
						TranslateAnimation downAnimation=new TranslateAnimation(0, 0, 0, height);
						downAnimation.setDuration(300);
						downAnimation.setAnimationListener(cellAnimationListener);
						upperGroup.startAnimation(downAnimation);
						
						TranslateAnimation upAnimation=new TranslateAnimation(0, 0, 0, -height);
						downAnimation.setDuration(300);
						downAnimation.setAnimationListener(cellAnimationListener);
						bottomGroup.startAnimation(upAnimation);
						selectedDailyInfoIndex=-1;
					}else
					{
						//to open up
						TranslateAnimation downAnimation=new TranslateAnimation(0, 0, 0, -height);
						downAnimation.setDuration(300);
						downAnimation.setAnimationListener(cellAnimationListener);
						upperGroup.startAnimation(downAnimation);
						
						TranslateAnimation upAnimation=new TranslateAnimation(0, 0, 0, height);
						downAnimation.setDuration(300);
						downAnimation.setAnimationListener(cellAnimationListener);
						bottomGroup.startAnimation(upAnimation);
						selectedDailyInfoIndex=position;
					}					
				}
			};
			upperGroup.setOnClickListener(onRegionClickListener);
			bottomGroup.setOnClickListener(onRegionClickListener);

			shareBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					shareDailyInfo();
				}
			});
			
			refuseBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					refuseDailyInfo();					
				}
			});
			
			favoriteBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					favoriteDailyInfo();					
				}
			});
			return convertView;
		}
	}
	
}
